package vsvm.gui.menus;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Iterator;

import javax.swing.JComponent;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JSeparator;

import vsvm.classifier.AbstractClassifier;
import vsvm.data.filter.NormalizeAttributeFilter;
import vsvm.data.model.AbstractAttribute;
import vsvm.gui.IActionsProvider;

public class AttributeMenu implements ActionListener {
	static String AC_FILTER = "filter";
	private AbstractAttribute attribute;
	
	public AttributeMenu(AbstractAttribute attr) {
		attribute = attr;
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getActionCommand().equals(AC_FILTER)) {
			//NormalizeAttributeFilter naf = new NormalizeAttributeFilter(attribute.getParent(), attribute);
			//naf.filter();			
		} else {
			((IActionsProvider)attribute).executeAction(e.getActionCommand());
		}
	}
	
	public static JPopupMenu getPopup(AbstractAttribute c) {
		JPopupMenu menu = new JPopupMenu();
		ArrayList<JComponent> items = createItems(c);
		for (Iterator<JComponent> iter = items.iterator(); iter.hasNext();) {
			menu.add(iter.next());			
		}
		
		return menu;	
	}
	
	private static ArrayList<JComponent> createItems(AbstractAttribute c) {
		ArrayList<JComponent> arr = new ArrayList<JComponent>();
		AttributeMenu dsm = new AttributeMenu(c);
		
		JMenuItem miFilter = new JMenuItem("Apply filter");
		miFilter.setActionCommand(AC_FILTER);
		miFilter.addActionListener(dsm);
		//arr.add(miFilter);
		
		if (c instanceof IActionsProvider) {
			IActionsProvider ap = (IActionsProvider)c;
			String[] act = ap.getActions();
			
			//if (act.length > 0)
				//arr.add(new JSeparator());
			
			for (int i = 0; i < act.length; i++) {
				JMenuItem mi = new JMenuItem(act[i]);
				mi.setActionCommand(act[i]);
				mi.addActionListener(dsm);
				arr.add(mi);
			}
		}
		
		return arr;
	}

}
